using System;
using System.Data;
using System.Data.SqlClient;

using Fan.Dongping.Common;
using Fan.Dongping.Common.DBMeta;
using Fan.Dongping.DataBase;

//using Microsoft.ApplicationBlocks.Data;

namespace Fan.Dongping.DataAccess
{
    /// <summary>
    /// Workflow DA
    /// </summary>
    public class WorkflowDA
    {
        /// <summary>
        /// Get all Workflows
        /// </summary>
        /// <returns></returns>
        public static DataTable GetWorkflows()
        {
            string sql = "select * from jkkb1_flw_nam";
            return DA_Base.RunReturnDataset(sql).Tables[0];
        }

        public static DataTable GetWorkflowsExistDoc()
        {
            string sql = "select distinct f.* from jkkb1_flw_nam f ,jkkb29_docu d where f.flw_no=d.flw_no";
            return DA_Base.RunReturnDataset(sql).Tables[0];
        }

        public static DataTable GetWorkflows(System.Collections.Generic.List<string> workflowNos)
        {
            if (workflowNos.Count == 0)
            {
                return null;
            }
            else
            {
                string cond = null;
                foreach (string var in workflowNos)
                {
                    cond += ",'" + var + "'";
                }
                cond = cond.Trim(new char[] { ',' });

                string sql = string.Format("select distinct f.* from jkkb1_flw_nam f where f.flw_no in ({0})", cond);

                return DA_Base.RunReturnDataset(sql).Tables[0];
            }
        }

        /// <summary>
        /// Get Workflow
        /// </summary>
        /// <param name="workflowID"></param>
        /// <returns></returns>
        public static DataRow GetWorkflow(int workflowID)
        {
            string sql = "select * from Workflow where WorkflowID =@WorkflowID ";

            SqlParameter[] para = new SqlParameter[1];
            para[0] = new SqlParameter("@WorkflowID", workflowID);
            DataSet ds = DA_Base.RunReturnDataset(sql, para);

            return ds.Tables[0].Rows[0];
        }

        public static string GetWorkflowName(string workflowNo)
        {
            string sql = "select flw_nam from jkkb1_flw_nam where flw_no =@WorkflowID ";

            SqlParameter[] para = new SqlParameter[1];
            para[0] = new SqlParameter("@WorkflowID", workflowNo);
            object o = DA_Base.ExecuteScalar(sql, para);
            if (o is DBNull)
            {
                return string.Empty;
            }
            else
            {
                return o as string;
            }
        }

        /// <summary>
        /// update
        /// </summary>
        /// <param name="workflowId"></param>
        /// <param name="currentStatus"></param>
        /// <param name="action"></param>
        /// <param name="nextStatus"></param>
        /// <returns></returns>
        public static int Update(int workflowId, string currentStatus, string action, string nextStatus)
        {
            string sql = "UPDATE workflow SET CurrentStatus=@CurrentStatus,ActionName=@ActionName,NextStatus=@NextStatus WHERE workflowID=@workflowID";
            SqlParameter[] para = new SqlParameter[4];
            para[0] = new SqlParameter("@workflowID", workflowId);
            para[1] = new SqlParameter("@CurrentStatus", currentStatus);
            para[2] = new SqlParameter("@ActionName", action);
            para[3] = new SqlParameter("@NextStatus", nextStatus);

            return DA_Base.ExecuteNonQuery(sql, para);
        }

        /// <summary>
        /// get actions
        /// </summary>
        /// <param name="currentStatus"></param>
        /// <param name="permissionName"></param>
        /// <returns></returns>
        public static DataTable GetActions(string currentStatus, string permissionName)
        {
            string sql = "select * from Workflow where CurrentStatus=@CurrentStatus and PermissionName=@Permission";
            SqlParameter[] paras = new SqlParameter[2];
            paras[0] = new SqlParameter("@CurrentStatus", currentStatus);
            paras[1] = new SqlParameter("@Permission", permissionName);

            return DA_Base.RunReturnDataset(sql, paras).Tables[0];
        }

        /// <summary>
        /// Get all actions
        /// </summary>
        /// <param name="currentStatus"></param>
        /// <returns></returns>
        public static DataTable GetActions(string currentStatus)
        {
            string sql = "select * from Workflow where CurrentStatus=@CurrentStatus";
            SqlParameter[] paras = new SqlParameter[1];
            paras[0] = new SqlParameter("@CurrentStatus", currentStatus);

            return DA_Base.RunReturnDataset(sql, paras).Tables[0];
        }

        /// <summary>
        /// get next status
        /// </summary>
        /// <param name="workflowID"></param>
        /// <returns></returns>
        public static string GetNextStatus(int workflowID)
        {
            string sql = "select NextStatus from Workflow where workflowID=@workflowID";
            //"select NextStatus from Workflow where CurrentStatus=@CurrentStatus and ActionName=@ActionName";
            SqlParameter[] paras = new SqlParameter[2];
            paras[0] = new SqlParameter("@workflowID", workflowID);
            // paras[1] = new SqlParameter("@ActionName", action);

            return DA_Base.ExecuteScalar(sql, paras) as string;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="workflowID"></param>
        /// <returns></returns>
        public static string GetNextResolution(int workflowID)
        {
            string sql = "select NextResolution from Workflow where workflowID=@workflowID";
            //"select NextStatus from Workflow where CurrentStatus=@CurrentStatus and ActionName=@ActionName";
            SqlParameter[] paras = new SqlParameter[2];
            paras[0] = new SqlParameter("@workflowID", workflowID);
            // paras[1] = new SqlParameter("@ActionName", action);

            return DA_Base.ExecuteScalar(sql, paras) as string;
        }


    }
}
